using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using FreeImageAPI;
using System.Drawing.Design;
using OpenFileDialogEx.Controls;
using OpenFileDialogEx.OS;

namespace SP
{
    public partial class OpenTextureDialog : OpenFileDialogEx.Controls.OpenFileDialogEx
    {
        public OpenTextureDialog()
        {
            InitializeComponent();
            this.OpenDialog.AutoUpgradeEnabled = false;
        }

        public override void OnFileNameChanged(string filePath)
        {
            if (filePath.ToLower().EndsWith(".dds") ||
                filePath.ToLower().EndsWith(".jpg") ||
                filePath.ToLower().EndsWith(".png") ||
                filePath.ToLower().EndsWith(".tga"))
            {
                if (preview.Image != null)
                    preview.Image.Dispose();

                LoadPreview(filePath);
            }
            else
            {
                if (preview.Image != null)
                    preview.Image.Dispose();
                preview.Image = null;
            }
        }

        private void LoadPreview(string filePath)
        {
            FreeImageEngine.Message += new OutputMessageFunction(FreeImageEngine_Message);

            try
            {
                //FileInfo fi = new FileInfo(filePath);
                //preview.Image = Bitmap.FromFile(filePath);
                // Format is stored in 'format' on successfull load.
                FREE_IMAGE_FORMAT format = FREE_IMAGE_FORMAT.FIF_UNKNOWN;

                // Try loading the file
                FIBITMAP dib = FreeImage.LoadEx(filePath, ref format);

                try
                {
                    // Error handling
                    if (!dib.IsNull)
                    {
                        preview.Image = FreeImage.GetBitmap(dib);
                    }
                }
                catch
                {
                }

                // Always unload bitmap
                FreeImage.UnloadEx(ref dib);
            }
            catch (Exception) { }

            FreeImageEngine.Message -= new OutputMessageFunction(FreeImageEngine_Message);
        }

        void FreeImageEngine_Message(FREE_IMAGE_FORMAT fif, string message)
        {
            System.Console.WriteLine(message);
        }

        public override void OnFolderNameChanged(string folderName)
        {
            if (preview.Image != null)
                preview.Image.Dispose();
            preview.Image = null;
        }

        public override void OnClosingDialog()
        {
            if (preview.Image != null)
            {
                preview.Image.Dispose();
                preview.Image = null;
            }
        }
    }

    internal class TextureEditor2 : UITypeEditor
    {
        private OpenTextureDialog texdlg = new OpenTextureDialog();

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            texdlg.StartLocation = AddonWindowLocation.Right;
            texdlg.DefaultViewMode = FolderViewMode.Details;
            texdlg.OpenDialog.AddExtension = true;

            //texdlg.OpenDialog.Filter = "DDS Texture Files|*.dds|Jpeg Texture Files|*.jpg";
            texdlg.OpenDialog.Filter = "DDS Texture Files|*.dds";

            texdlg.OpenDialog.FileName = value.ToString();
            if (texdlg.OpenDialog.FileName.Length > 0)
                texdlg.OpenDialog.InitialDirectory = System.IO.Path.GetDirectoryName(texdlg.OpenDialog.FileName);

            if (texdlg.ShowDialog(System.Windows.Forms.Form.ActiveForm) == DialogResult.OK)
            {
                return texdlg.OpenDialog.FileName;
            }

            return base.EditValue(context, provider, value);
        }
    }
}

